package com.spring.schedule_system.utils;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.metadata.WriteSheet;
import lombok.Getter;

import java.io.OutputStream;
import java.util.Collection;
import java.util.List;

@Getter
public class ExcelWriterBO {

    private ExcelWriter excelWriter;

    public ExcelWriterBO(OutputStream outputStream) {
        // 创建写入流
        ExcelWriterBuilder excelWriterBuilder = EasyExcelFactory.write(outputStream);
        this.excelWriter = excelWriterBuilder.build();
    }

    /**
     * 创建sheet页
     * @param sheetName
     * @param headerClass
     * @return
     */
    public WriteSheet createSheet(String sheetName, Class<?> headerClass){
        WriteSheet writeSheet = EasyExcelFactory.writerSheet(sheetName).head(headerClass).build();
        return writeSheet;
    }

    // 写入数据
    public void write(List<?> dataList, WriteSheet writeSheet){
        excelWriter.write(dataList,writeSheet);
    }

    // 写入完成
    public void finish(){
        if (excelWriter != null) {
            // 关闭流
            excelWriter.finish();
        }
    }

}
